//
//  AY_BookOpinionPage.m
//  AiYue
//
//  Created by boWen on 2023/11/17.
//

#import "AY_BookOpinionPage.h"
#import "AY_BookYiJCell1.h"
#import "UITextView+Placeholder.h"
#import "IQKeyboardManager.h"

@interface AY_BookOpinionPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) UILabel * wtLabel;
@property (nonatomic, strong) NSArray * typeList;
@property (nonatomic, strong) UITextView * contentTView;
@property (nonatomic, strong) AY_BookYiJCell1 * cell;
@end

@implementation AY_BookOpinionPage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithNav];
}


- (void)initWithNav {
    self.gk_navTitle = @"意见反馈";
    [self.view addSubview:self.baseTableView];
    [self setupTopView];
    [self setupDownView];
}


- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT - YNUM(77) - GK_SAFEAREA_BTM - GK_STATUSBAR_NAVBAR_HEIGHT)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor whiteColor];
        [myTableView registerClass:[AY_BookYiJCell1 class] forCellReuseIdentifier:@"AY_BookYiJCell1"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (NSArray *)typeList {
    if(!_typeList) {
        _typeList = @[@"意见反馈", @"功能使用", @"书籍相关", @"优化建议", @"账号相关", @"功能使用"];
    }
    return _typeList;
}

- (void)setupTopView {
    UIView * bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, YNUM(356))];
    UIImageView * topImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_feedBack.png") andBg:[UIColor clearColor] andRadius:YNUM(1) andAddSubView:bg];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bg);
        make.left.equalTo(bg);
        make.right.equalTo(bg);
        make.height.offset(YNUM(120));
    }];
    
    UILabel * changjingLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"问题场景" andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:bg];
    [changjingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage.mas_bottom).offset(YNUM(5));
        make.left.equalTo(bg).offset(YNUM(12));
        make.height.mas_offset(YNUM(22));
    }];
    
    UIView * changjView = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#FAFAFA") andRadius:YNUM(6) andAddSubView:bg];
    [bg addSubview:changjView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeTypeAction)];
    [changjView addGestureRecognizer:tap];
    [changjView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(changjingLabel.mas_bottom).offset(YNUM(5));
        make.left.equalTo(bg);
        make.right.equalTo(bg);
        make.height.mas_offset(YNUM(40));
    }];
    
   self.wtLabel  = [AYTool createLabelWithFrame:CGRectZero andText:@"请选择问题发生的场景" andTextColor:COLOR(@"#BFBFBF") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:bg];
    [self.wtLabel sizeToFit];
    [self.wtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(16));
        make.left.equalTo(changjView).offset(YNUM(14));
        make.centerY.equalTo(changjView);
    }];

    UIImageView * arrowImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_arrowNight.png") andBg:[UIColor whiteColor] andRadius:0.0 andAddSubView:changjView];
    [arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(16));
        make.width.mas_equalTo(YNUM(16));
        make.right.equalTo(changjView).offset(YNUM(-14));
        make.centerY.equalTo(changjView);
    }];
    
    UILabel * contentLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"问题描述" andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:bg];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(changjView.mas_bottom).offset(YNUM(5));
        make.left.equalTo(bg).offset(YNUM(12));
        make.height.mas_offset(YNUM(22));
    }];
    
    UITextView * contentTextV = [[UITextView alloc] init];
    contentTextV.backgroundColor = COLOR(@"#FAFAFA");
    contentTextV.placeholder = @"请描述下遇到的问题";
    contentTextV.font = [UIFont systemFontOfSize:YNUM(12)];
    contentTextV.layer.cornerRadius = YNUM(8);
    contentTextV.layer.masksToBounds = YES;
    [bg addSubview:contentTextV];
    [contentTextV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentLabel.mas_bottom).offset(YNUM(12));
        make.left.equalTo(bg).offset(YNUM(12));
        make.right.equalTo(bg).offset(YNUM(-12));
        make.height.mas_offset(YNUM(100));
    }];
    self.contentTView = contentTextV;
    
    self.baseTableView.tableHeaderView = bg;
}

- (void)setupDownView {
    UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, GK_SCREEN_HEIGHT - YNUM(77) - GK_SAFEAREA_BTM, SCREEN_W, YNUM(77) + GK_SAFEAREA_BTM)];
    footerV.backgroundColor = [UIColor whiteColor];
    UIButton * sureBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"确认提交" andFont:[UIFont boldSystemFontOfSize:16] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(handleSureNetAction) andRadius:YNUM(8) andAddSubView:footerV];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(footerV).offset(YNUM(-16-GK_SAFEAREA_BTM));
        make.left.equalTo(footerV).offset(YNUM(16));
        make.right.equalTo(footerV).offset(YNUM(-16));
        make.height.mas_offset(YNUM(45));
    }];
    [self.view addSubview:footerV];
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    _cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookYiJCell1"];
    return _cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(300);
}


#pragma mark - 事件

- (void)handleSureNetAction {
    if(kStringIsEmpty(self.wtLabel.text) || kStringIsEmpty(self.contentTView.text) || kStringIsEmpty(_cell.wwxField.text)) {
        ShowToast(@"请先填写必填项~");
        return;
    }
    [AYApiRequest postSuggestionSuggestionType:self.wtLabel.text content:self.contentTView.text qqOrWx:_cell.wwxField.text mobile:kStringUtils(_cell.phoneField.text) Success:^(AY_QiuBookModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            ShowToast(@"感谢您的反馈~");
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}


- (void)changeTypeAction {
    UIAlertController *_alertVC = [UIAlertController alertControllerWithTitle:@"选择问题发生的场景" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
        Weak_Self
        UIAlertAction *_doAction = [UIAlertAction actionWithTitle:self.typeList[0] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [weakSelf changeColor:self.typeList[0]];
        }];
        [_alertVC addAction:_doAction];

        UIAlertAction *_doAction1 = [UIAlertAction actionWithTitle:self.typeList[1] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [weakSelf changeColor:self.typeList[1]];
        }];
        [_alertVC addAction:_doAction1];
    
        UIAlertAction *_doAction2 = [UIAlertAction actionWithTitle:self.typeList[2] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [weakSelf changeColor:self.typeList[2]];
        }];
        [_alertVC addAction:_doAction2];
    
        UIAlertAction *_doAction3 = [UIAlertAction actionWithTitle:self.typeList[3] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [weakSelf changeColor:self.typeList[3]];
        }];
        [_alertVC addAction:_doAction3];
    
        UIAlertAction *_doAction4 = [UIAlertAction actionWithTitle:self.typeList[4] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [weakSelf changeColor:self.typeList[4]];
        }];
        [_alertVC addAction:_doAction4];
          
        UIAlertAction *_doAction5 = [UIAlertAction actionWithTitle:self.typeList[5] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [weakSelf changeColor:self.typeList[5]];
        }];
        [_alertVC addAction:_doAction5];
    
        UIAlertAction * _cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [_alertVC addAction:_cancle];
             
         
        [self presentViewController:_alertVC animated:YES completion:nil];
}

- (void)changeColor:(NSString *)text {
    self.wtLabel.text = text;
    self.wtLabel.textColor = COLOR(@"#010101");
    self.wtLabel.font = [UIFont boldSystemFontOfSize:YNUM(14)];
}

@end
